package com.zhj.leetcode;

/**
 * 力扣 203 链表删除指定节点
 * @author zhj
 */
public class Test203 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(6, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6)))))));
        ListNode listNode = remove2(head, 6);
        System.out.println(head);

    }

    private static void remove1(ListNode head, int val) {
        ListNode cur = head;
        while (cur!= null && cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            }
            System.out.println(cur.val);
            cur = cur.next;
            if (head.val == val) {
                head.next = null;
            }
        }
    }

    private static ListNode remove2(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy;
        while (head!= null) {
            if (head.val == val) {
                prev.next = head.next;
                head = head.next;
            } else {
                prev = head;
                head = head.next;
            }
        }
        return dummy.next;
    }
}
